<template>
    <div class="xd-ckplayer-video">
        <div :id="id" class="video"></div>
    </div>
</template>
<script>
// var cookie = {
//   set: function (name, value) {
//     var Days = 30
//     var exp = new Date()
//     exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000)
//     document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
//   },
//   get: function (name) {
//     /* eslint-disable */
//     var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
//     if (arr = document.cookie.match(reg)) {
//       return unescape(arr[2])
//     } else {
//       return null
//     }
//   },
//   del: function (name) {
//     var exp = new Date();
//     exp.setTime(exp.getTime() - 1)
//     var cval = getCookie(name)
//     if (cval != null) {
//       document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString()
//     }
//   }
// }

export default {
    name: 'CkplayerVideo',
    props: {
        videoUrl: {
            type: String,
            default: ''
        },
        stime: {
            type: Number,
            default: 0
        },
        timeScheduleAdjust: {
            type: Number,
            default: 5
        }
    },
    watch: {
        timeScheduleAdjust() {
            this.init();
        }
    },
    data() {
        return {
            id: 'ckplayer' + new Date().getTime(),
            playerObj: null,
            duration: 0,
            videoId: this.videoUrl,
            isTip: false, 
        };
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            // var cookieTime = cookie.get('time_' + this.videoUrl)
            // if (!cookieTime || cookieTime == undefined) {
            //   cookieTime = 0
            // }
            // if (cookieTime > 0) {
            //   this.$message({
            //     message: '上次观看时间(秒)为：' + cookieTime + '，已加载到当前时间。',
            //     type: 'warning'
            //   })
            // }
            var videoObject = {
                container: '#' + this.id,
                variable: 'player',
                video: this.videoUrl,
                autoplay: true,
                playbackrate: 1, 
                timeScheduleAdjust: this.timeScheduleAdjust
            };
            if (this.stime > 0) {
                // 如果记录时间大于0，则设置视频播放后跳转至上次记录时间
                videoObject['seek'] = this.stime;
            }
            // eslint-disable-next-line
            this.playerObj = new ckplayer(videoObject);
            this.playerObj.addListener('duration', this.durationHandler);
            this.playerObj.addListener('time', this.timeHandler);
            this.playerObj.addListener('playbackRate', this.playbackRateHandler);//改变倍速
        },
        quitFullScreen() {
            try {
                this.playerObj.quitFullScreen();
            } catch (error) {
                console.log(error);
            }
        },
        play() {
            this.playerObj.videoPlay();
        },
        pause() {
            this.playerObj.videoPause();
        },
        durationHandler(duration) {
            this.duration = duration;
        },
        playbackRateHandler(objNum){ //改变倍速
            this.$emit("playbackRateHandler",objNum[0]);
        },
        timeHandler(time) {
            if (location.href.indexOf('Cinziqao/Detail') > -1) {//心理科普
                return
            }
            if (location.href.indexOf('LearningHome/Learning/VideoCourse') === -1) {
                this.playerObj.videoPause();
                // return
            }
            // console.log('当前（秒）：' + time)
            // cookie.set('time_' + this.videoUrl, time)
        },
        clear() {
            this.playerObj.videoPause();
        }
    }
};
</script>
<style lang="less">
.xd-ckplayer-video {
    margin-top: 15px;
}
</style>